package com.balabala.gym_system.dao.Impl;

import com.balabala.gym_system.dao.AdvertisingDao;
import com.balabala.gym_system.pojo.dto.AdvertisingDto;
import com.balabala.gym_system.pojo.entity.Advertising;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.regex.Pattern;
@Component
public class AdvertisingDaoImpl implements AdvertisingDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insertAd(Advertising advertising) {
        mongoTemplate.save(advertising);
    }

    @Override
    public void updateAd(Advertising advertising) {
        mongoTemplate.save(advertising);
    }

    @Override
    public void deleteById(String id) {
        Query query=new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Advertising.class);
    }

    @Override
    public void deleteByIds(List<String> ids) {
//        Query query=new Query(Criteria.where("ids").is(ids));
//        mongoTemplate.remove(query,Advertising.class);
        for (String id:ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Advertising> findByStatus(Integer status) {
        Query query=new Query(Criteria.where("status").is(status));
        List<Advertising> advertisingList = mongoTemplate.find(query, Advertising.class);
        return advertisingList;
    }

    @Override
    public List<Advertising> findAll() {
         List<Advertising> advertisingList = mongoTemplate.findAll(Advertising.class);
         return advertisingList;
    }

    @Override
    public Advertising findById(String id) {
         Advertising advertising = mongoTemplate.findById(id, Advertising.class);
         return advertising;
    }

    @Override
    public List<Advertising> findAdByAdvertisingDto(AdvertisingDto advertisingDto) {
        //构建查询条件
        Query query=new Query();

        //根据位置&标题进行模糊查询
        if(advertisingDto.getPosition()!=null){
            query.addCriteria(Criteria.where("position").is(advertisingDto.getPosition()));
        }
        //广告名称不为空
        if (!StringUtils.isEmpty(advertisingDto.getAdvertisingName())){
            //Pattern pattern = Pattern.compile("^.*张.*$", Pattern.CASE_INSENSITIVE);
            Pattern pattern =Pattern.compile("^.*"+advertisingDto.getAdvertisingName()+".*$",Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("advertisingName").is(pattern));
        }

        //设置分页条件
        query.skip((advertisingDto.getPage()-1)*advertisingDto.getLimit());//开始序号
        query.limit(advertisingDto.getLimit());//分页的数据有几条

        //页面数据
        List<Advertising> advertisingList = mongoTemplate.find(query, Advertising.class);
        return advertisingList;
    }

    @Override
    public Long findAdByAdvertisingDtoCount(AdvertisingDto advertisingDto) {
        //构建查询条件
        Query query=new Query();

        //根据位置&标题进行模糊查询
        if(advertisingDto.getPosition()!=null){
            query.addCriteria(Criteria.where("position").is(advertisingDto.getPosition()));
        }
        //广告名称不为空
        if (!StringUtils.isEmpty(advertisingDto.getAdvertisingName())){
            //Pattern pattern = Pattern.compile("^.*张.*$", Pattern.CASE_INSENSITIVE);
            Pattern pattern =Pattern.compile("^.*"+advertisingDto.getAdvertisingName()+".*$",Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("advertisingName").is(pattern));
        }

        Long count = mongoTemplate.count(query, Advertising.class);
        return count;
    }

    @Override
    public List<Advertising> findAdByPosition(Integer position) {
       Query query=new Query(Criteria.where("position").is(position));
       List<Advertising> advertisingList = mongoTemplate.find(query, Advertising.class);
       return advertisingList;
    }
}
